package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.*
import kotlinx.android.synthetic.main.type6_formula2_location_activity.*
import kotlinx.android.synthetic.main.type6_formula3_location_activity.*
import kotlinx.android.synthetic.main.type6_formula4_location_activity.*
import kotlinx.android.synthetic.main.type6_formula5_location_activity.*
import java.lang.NumberFormatException

/**
 *  系统设计 - 天花喇叭覆盖范围计算
 */
class Type6Formula5Activity : BaseActivity() {
    var type = 1

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type6Formula5Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type6_formula5_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula6_5_a_TEV.reset()
            formula6_5_b_TEV.reset()
            formula6_5_c_TEV.reset()
            formula6_5_d_TEV.reset()
            formula6_5_e_TEV.reset()
            formula6_5_f_TEV.reset()
            formula6_5_g_TEV.reset()
            formula6_5_h_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                var resultE: Double?
                if (radioButton21.isSelect()) {
                    resultE = FormulaUtil.formula6_5_1(
                        formula6_5_b_TEV.getDouble(),
                        1.2, formula6_5_a_TEV.getDouble()
                    )
                } else {
                    resultE = FormulaUtil.formula6_5_1(
                        formula6_5_b_TEV.getDouble(),
                        1.7, formula6_5_a_TEV.getDouble()
                    )
                }
                var resultF = FormulaUtil.formula6_5_2(
                    formula6_5_d_TEV.getDouble(),
                    resultE, type
                )
                var resultG = FormulaUtil.formula6_5_3(resultE, type)
                var resultH: Double?
                if (radioButton21.isSelect()) {
                    resultH = FormulaUtil.formula6_5_4(
                        formula6_5_c_TEV.getDouble(),
                        formula6_5_b_TEV.getDouble(),
                        1.2
                    )
                } else {
                    resultH = FormulaUtil.formula6_5_4(
                        formula6_5_c_TEV.getDouble(),
                        formula6_5_b_TEV.getDouble(),
                        1.7
                    )
                }
                formula6_5_e_TEV.setText(resultE)
                formula6_5_f_TEV.setText(resultF,0)
                formula6_5_g_TEV.setText(resultG)
                formula6_5_h_TEV.setText(resultH)
            } catch (e: NumberFormatException) {
                formula6_5_e_TEV.setText("Infinity")
                formula6_5_f_TEV.setText("Infinity")
                formula6_5_g_TEV.setText("Infinity")
                formula6_5_h_TEV.setText("Infinity")
            }
        }
        radioButton11.setOnClickListener {
            type = 1
            radioButton11.setSelect(true)
            radioButton12.setSelect(false)
            radioButton13.setSelect(false)
        }
        radioButton12.setOnClickListener {
            type = 2
            radioButton11.setSelect(false)
            radioButton12.setSelect(true)
            radioButton13.setSelect(false)
        }
        radioButton13.setOnClickListener {
            type = 3
            radioButton11.setSelect(false)
            radioButton12.setSelect(false)
            radioButton13.setSelect(true)
        }
        radioButton21.setOnClickListener {
            radioButton21.setSelect(true)
            radioButton22.setSelect(false)
        }
        radioButton22.setOnClickListener {
            radioButton21.setSelect(false)
            radioButton22.setSelect(true)
        }
    }
}